package com.hmall.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * GatewayFilter
 *
 * @Author wd
 * @Date 2025/6/20 11:22
 */
@Slf4j
public class MyFilterGatewayFilterFactory extends AbstractGatewayFilterFactory {

    @Override
    public GatewayFilter apply(Object config) {
        return new GatewayFilter() {

            /**
             * 针对请求的过滤处理逻辑
             * @param exchange
             * @param chain
             * @return
             */
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {


                log.info("收到请求，请求路径：{}",exchange.getRequest().getURI().getPath() );

                //放行请求
                return chain.filter(exchange);


                //拒绝请求，直接响应结束
//                exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);//响应401
//                return exchange.getResponse().setComplete();
            }
        };


    }
}
